;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var dialog = require("dialog");
	var base = require("base");
	var router = require("../hash/pageRouter")
	var css_permisionManage =require("./permisionManage.css");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	
	var init = function() {
		initEvent();
		getPermission();
	};
	var initEvent = function() {
	/*	//修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");
		$("#airportCodeMainten", window.parent.document).addClass("head_link_1");*/
		//删除弹框提示
		$("#permissionListTable").on("click", ".delete_btn", function() {
			var roleId = $(this).parents("tr.table-data").attr("roleId");
			var name = $(this).parents("tr.table-data").attr("roleName");
			$("#delete_item_dialog .delete_item_contents").html('确定要删除“' + roleId + '”权限组吗？');
			deleteItem(roleId);
		});

		//编辑权限
		$("#permissionListTable").on("click", ".edit_link", function() {
			var roleId = $(this).parents("tr.table-data").attr("roleId");
			router.route("permisionGroupAdd1/permisionGroupAdd.html?roleId=" + roleId);
		});

		$("#add_permision_btn").unbind("click").click(function() {
			router.route("permisionGroupAdd1/permisionGroupAdd.html");
		});
	};
	//删除弹框
	var deleteItem = function(roleId) {
		var deleteItemDialog = $("#delete_item_dialog");
		// 调用dialog中的方法显示dialog，才能用dialog其中的方法关闭与取消dialog
		dialog.showDialog(deleteItemDialog, function() {
			// 确定按钮要做的事
			permissionDelete(roleId);
			dialog.hideDialog(deleteItemDialog);
		});
	};
	//删除权限接口
	var permissionDelete = function(roleId) {
		base.fnAjax({
			action: "deleteRole.json",
			oData: {
				roleId: roleId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					router.route("permisionManage/permisionManage.html");
				} else {
					util.alert("删除失败");
				}
				//调用修改变化后的权限再次刷新页面
				//              main.setPermissionLoaded();
				//              main.loadPermission(permission.init);
				init();
			}
		});
	};
	// 获取权限内容
	var getPermission = function() {
		
		base.fnAjax({
			action: "inquireRoleList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$(".nofindclass").text("未找到数据");
					if (data.datas.length > 0) {
						$(".nofindclass").addClass('none');
					} else {
						$(".nofindclass").removeClass('none');
					};
					var tpl = $("#permisionListTpl").html();
					var html = juicer(tpl, data);
					$(".permisionBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#permisionPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							getPermission();
						});
				} else {
					$(".nofindclass").text("出错了");
					util.error("信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});